import os
from flask import Flask
from flask_migrate import upgrade, stamp
from .models import db, Admin, Questionnaire

def register_commands(app: Flask):
    @app.cli.command()
    def deploy():
        upgrade()

        if Admin.query.count() <= 1:
            admin1 = Admin(name=os.getenv('ADMIN1_NAME'))
            admin1.set_password(os.getenv('ADMIN1_PASSWORD'))
            admin2 = Admin(name=os.getenv('ADMIN2_NAME'))
            admin2.set_password(os.getenv('ADMIN2_PASSWORD'))
            db.session.add(admin1)
            db.session.add(admin2)
            db.session.commit()

    @app.cli.command()
    def forge():
        from .fake import fakes
        db.drop_all()
        db.create_all()
        stamp()
        fakes(10)

    @app.cli.command()
    def convert():
        from sqlalchemy import create_engine
        import pandas.io.sql as sql
        import os
        con = create_engine(os.getenv('DATABASE_URL'))
        df = sql.read_sql(f'select * from {Questionnaire.__table__}',con)
        df.to_excel('ds.xls')
